<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/9/9
 * Time: 16:17
 */

namespace app\admin\controller;


use think\Db;

class Lottery extends Base
{
    public $table = 'lottery';

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $keyword = input('keyword');

        if($keyword)
            $where['name'] = ['like','%'.$keyword.'%'];

        if(!isset($where))
            $where = '1=1';
        $data['list'] = Db::name($this->table)
            ->where('site_id',$this->site_id)
            ->where($where)
            ->order('id desc')
            ->paginate();

        $data['page'] = $data['list']->render();

        $data['new'] = [];
        if($data['list']){
            foreach ($data['list'] as $key=>$value){
                $temp = $value;
                $temp['link_url'] = $this->getLinkUrl($value['id']);
                $data['new'][$key] = $temp;
            }
        }


        $this->assign('data',$data);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            return $this->save();
        }
        $this->assign('data',1);
        $this->assign('title','新增');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        if(request()->isPost()){
            return $this->save();
        }
        $data['link_url'] = $this->getLinkUrl($id);
        $data['start'] = date('Y-m-d H:i:s',$data['start']);
        $data['end'] = date('Y-m-d H:i:s',$data['end']);
        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function getLinkUrl($id)
    {
        $admin = session("admin");
        return \request()->domain()."/index/lottery/index/site/".$admin['site_id']."/id/".$id;
    }

    public function save()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'name'=>input('name'),
            'remark'=>input('remark'),
            'lottery_type'=>input('lottery_type/d'),
            'nums'=>input('nums/d'),
            'head_img'=>input('head_img'),
            'background_img'=>input('background_img'),
            'prize_img'=>input('prize_img'),
            'rule'=>input('rule'),
            'start'=>strtotime(input('start')),
            'end'=>strtotime(input('end')),
            'is_ip'=>input('is_ip'),
            'ip_nums'=>input('ip_nums'),
            'is_device'=>input('is_device'),
            'device_nums'=>input('device_nums'),
            'enable'=>input('enable/d'),
            'modtime'=>time(),
        ];
        if($id){
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            $data['addtime'] = time();
            $data['site_id'] = $this->site_id;
            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function ajaxupdate()
    {
        $type = input('type');
        $field = input('field');
        $data[$field] = input('value/d');
        $id = input('id/d');
        $data['modtime'] = time();
        $g = Db::name($this->table)->where('id',$id)->find();
        if(!$g)
            return dataReturn(0,'不存在');
        if($field == 'extra'){
            $data['total'] = $g['sale'] + $data['extra'];
        }
        $a = Db::name($this->table)->where('id',$id)->update($data);
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
    }

    public function getAllType()
    {
        $config['difficulty'] = Db::name('difficulty')->select();
        $config['use'] = Db::name('use')->select();
        $config['column'] = Db::name('column')->select();
        $config['color'] = config('app.course_color');
        return $config;
    }

}